//
//  ByCellMyCoupons.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/10/26.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByCellMyCoupons.h"

@implementation ByCellMyCoupons
{
    UIImageView *_bgView;               // 背景
    UILabel     *_moneyLabel;           // 金额
    UILabel     *_validDateLabel;       // 有效期
    UILabel     *_kindLabel;            // 种类
    UILabel     *_useConditionLabel;    // 使用条件
    UILabel     *_getConditionLabel;    // 领取条件
    UILabel     *_statusLabel;          // 状态
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    // 背景
    _bgView = [UIImageView new];
    _bgView.image = [UIImage imageWithColor:[UIColor orangeColor]];
    [self.contentView addSubview:_bgView];
    
    // 下面的视图都是基于背景的（是背景的子视图）
    // 金额
    _moneyLabel = [[UILabel alloc] init];
    _moneyLabel.adjustsFontSizeToFitWidth = true;
    _moneyLabel.text = @"￥99";
    _moneyLabel.font = FONT(50);
    _moneyLabel.textAlignment = NSTextAlignmentCenter;
    _moneyLabel.textColor = [UIColor whiteColor];
    [_bgView addSubview:_moneyLabel];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:@"￥99"];
    [attr addAttribute:NSFontAttributeName value:FONT(24) range:NSMakeRange(0, 1)];
    _moneyLabel.attributedText = attr;

    
    // 有效期
    _validDateLabel = [[UILabel alloc] init];
    _validDateLabel.adjustsFontSizeToFitWidth = true;
    _validDateLabel.text = @"有效期：剩余3天";
    _validDateLabel.font = FONT(12);
    _validDateLabel.textAlignment = NSTextAlignmentCenter;
    _validDateLabel.textColor = BY_RED_COLOR;
    [_bgView addSubview:_validDateLabel];
    
    // 种类
    _kindLabel = [[UILabel alloc] init];
    _kindLabel.adjustsFontSizeToFitWidth = true;
    _kindLabel.text = @"全品类";
    _kindLabel.font = FONT(16);
    _kindLabel.textAlignment = NSTextAlignmentLeft;
    _kindLabel.textColor = [UIColor whiteColor];
    [_bgView addSubview:_kindLabel];
    
    // 使用条件
    _useConditionLabel = [[UILabel alloc] init];
    _useConditionLabel.adjustsFontSizeToFitWidth = true;
    _useConditionLabel.text = @"无使用条件";
    _useConditionLabel.font = FONT(16);
    _useConditionLabel.textAlignment = NSTextAlignmentLeft;
    _useConditionLabel.textColor = [UIColor whiteColor];
    [_bgView addSubview:_useConditionLabel];
    
    // 分割线
    UIView *line = [UIView new];
    line.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8];
    [_bgView addSubview:line];
    
    // 领取条件
    _getConditionLabel = [[UILabel alloc] init];
    _getConditionLabel.adjustsFontSizeToFitWidth = true;
    _getConditionLabel.text = @"领取条件：连续签到30天";
    _getConditionLabel.font = FONT(14);
    _getConditionLabel.textAlignment = NSTextAlignmentLeft;
    _getConditionLabel.textColor = [UIColor blackColor];
    [_bgView addSubview:_getConditionLabel];
    
    // 状态
    _statusLabel = [[UILabel alloc] init];
    _statusLabel.backgroundColor = BY_THEME_COLOR;
    _statusLabel.numberOfLines = 0;
    _statusLabel.text = @"未\n使\n用";
    _statusLabel.font = FONT(24);
    _statusLabel.textAlignment = NSTextAlignmentCenter;
    [_bgView addSubview:_statusLabel];
    
    
    // 布局
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(5, 10, 5, 10));
    }];
    [_moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(_bgView).multipliedBy(1.0 / 3);
        make.top.bottom.left.equalTo(@0);
    }];
    [_validDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_moneyLabel);
        make.bottom.equalTo(@0);
        make.height.equalTo(@20);
    }];
    [_statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.equalTo(@0);
        make.width.equalTo(_moneyLabel).multipliedBy(2.0 / 3);
    }];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@0.5);
        make.left.equalTo(_moneyLabel.mas_right);
        make.right.equalTo(_statusLabel.mas_left).offset(-15);
        make.centerY.equalTo(_statusLabel).multipliedBy(1.2);
    }];
    [_useConditionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@20);
        make.left.right.equalTo(line);
        make.bottom.equalTo(line.mas_top).offset(-6);
    }];
    [_kindLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.height.right.equalTo(_useConditionLabel);
        make.bottom.equalTo(_useConditionLabel.mas_top).offset(-3);
    }];
    [_getConditionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(_useConditionLabel);
        make.top.equalTo(line.mas_bottom).offset(6);
    }];
}

- (void)setModel:(ByModelCoupons *)model {
    _model = model;
    
    // 金额
    NSString *money = [NSString stringWithFormat:@"￥%li", _model.money];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:money];
    [attr addAttribute:NSFontAttributeName value:FONT(24) range:NSMakeRange(0, 1)];
    _moneyLabel.attributedText = attr;
    
    // 有效期
    NSString *remainDay = [NSString stringWithFormat:@"有效期：剩余%li天", _model.remainDay];
    _validDateLabel.text = remainDay;
    
    // 状态
    if (_model.status == ByCouponsStatusAvailable) {
        _statusLabel.text = @"未\n使\n用";
        _statusLabel.textColor = BY_GOLD_COLOR;
    }
    else {
        _statusLabel.textColor = [UIColor lightGrayColor];
        
        if (_model.status == ByCouponsStatusUsed) {
            _statusLabel.text = @"已\n使\n用";
        }
        else if (_model.status == ByCouponsStatusExpired) {
            _statusLabel.text = @"已\n过\n期";
        }
    }

}

@end
